package com.liesheng.haylou.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;

import com.liesheng.haylou.R;

import org.greenrobot.eventbus.EventBus;


public abstract class BaseFragment<T extends ViewDataBinding, V extends BaseVm> extends BaseFunFragment {

    protected T dataBinding;


    protected V mVm;

    private View layout_empty, layout_head, btn_empty;
    private TextView tv_title;
    private LinearLayout content;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = setContentView();
        initData();
        mVm = getViewModel();
        if (isRegistEventBus())
            EventBus.getDefault().register(this);
        return view;
    }


    private View setContentView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_base, null);
        content = view.findViewById(R.id.content);
        dataBinding = getDataBinding();
        content.addView(dataBinding.getRoot());
        layout_empty = view.findViewById(R.id.layout_empty);
        layout_head = view.findViewById(R.id.layout_head);
        tv_title = view.findViewById(R.id.tv_title);
        btn_empty = view.findViewById(R.id.btn_empty);
        btn_empty.setOnClickListener(onClickListener);
        return view;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_empty:
                    onClickEmptyBtn();
                    break;
            }
        }
    };

    //无数据时点击按钮
    protected void onClickEmptyBtn() {

    }

    public void setTitle(String title) {
        tv_title.setText(title);
        layout_head.setVisibility(View.VISIBLE);
    }

    public void setTitle(int title) {
        tv_title.setText(title);
        layout_head.setVisibility(View.VISIBLE);
    }

    public void setHeadLayoutViesible(boolean visible) {
        layout_head.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    protected abstract T getDataBinding();

    public abstract V getViewModel();

    protected abstract void initData();

    public abstract void onClickView(View view);

    protected boolean isRegistEventBus() {
        return false;
    }

    //控制空白提示页面显示
    protected void showEmptyLayout(boolean isShow) {
        layout_empty.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mVm != null) {
            mVm.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mVm != null) {
            mVm.onPause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mVm != null) {
            mVm.onDestroy();
        }
        unsubscribe();
        EventBus.getDefault().unregister(this);
    }
}
